package com.tjsinfo.tjsoa.meetingroom.bo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.Dao.JDBCDao;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.meetingroom.vo.MeetingIncept;
import com.tjsinfo.tjsoa.meetingroom.vo.MeetingRoomEmploy;
import com.tjsinfo.tjsoa.message.TjsMessageIm;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

public class MeetingRoomInceptBo implements IMeetingRoomInceptBo {
	private IQbcDao qbcDao;
	private JDBCDao jdbcdao;

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.meetingroom.bo.IMeetingRoomInceptBo#getQbcDao()
	 */
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.meetingroom.bo.IMeetingRoomInceptBo#setQbcDao(com.tjsinfo.tjsoa.Dao.IQbcDao)
	 */
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.meetingroom.bo.IMeetingRoomInceptBo#findByReceiveUser(com.tjsinfo.tjsoa.system.vo.TSystemUser)
	 */
	public List findByReceiveUser(TSystemUser tjsUser) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MeetingIncept.class);
		criteria.add(Restrictions.eq("receiveUserId", tjsUser.getUserId()));
		criteria.addOrder(Order.asc("state"));
		criteria.addOrder(Order.desc("id"));
		List list = qbcDao.getCriteria(criteria);
		return list;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.meetingroom.bo.IMeetingRoomInceptBo#save(java.lang.String, java.lang.Integer)
	 */
	public void update(String people,String employ_id,Date datetime,String sendUsername,String meetingTime) {
		String delsql = "delete from t_meetingroom_incept where employ_id='"+employ_id+"'";
		jdbcdao.executeUpdate(delsql);
		
		this.save(people, employ_id, datetime, sendUsername, meetingTime);
	}
	
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.meetingroom.bo.IMeetingRoomInceptBo#save(java.lang.String, java.lang.Integer)
	 */
	public void save(String people,String employ_id,Date datetime,String sendUsername,String meetingTime) {
		/**
	     * 循环主送，区分当前系统数据库nowList，并将其他数据库的人员分类存储sqlMap
	     */
		try {
			String[] peoples = people.split(",");
			Connection conn = jdbcdao.getConn();
			String stmtSQL = "insert into t_meetingroom_incept(id,employ_id,receiveUser_id,receiveUser_name,start_time,state) values(?,?,?,?,?,?)";
			PreparedStatement statement = null;
			
			statement = conn.prepareStatement(stmtSQL,Statement.RETURN_GENERATED_KEYS);
			for (int i = 0; i < peoples.length; i++) {
				if (peoples[i]!=null && !"".equals(peoples[i])) {
					UUID uuid = UUID.randomUUID();
	             	statement.setString(1, uuid.toString().replace("-", ""));
				 	statement.setString(2, employ_id);
	             	statement.setInt(3, Integer.valueOf(peoples[i]));
	             	statement.setString(4, "");
				 	java.sql.Date sd= new  java.sql.Date(datetime.getTime());
				 	statement.setDate(5, sd);
	             	statement.setInt(6, 0);
	             	statement.execute(); 
				}
			}
			jdbcdao.closeStmt();
			Connection con = jdbcdao.getConn();
			try {
				if (!con.isClosed()) {
					jdbcdao.closeConn();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				
			/**
		     * 发送提醒
		     */
			for (int i = 0; i < peoples.length; i++) {
				if (peoples[i]!=null && !"".equals(peoples[i])) {
//					TjsMessage message1 = new TjsMessage();
//					message1.setSendusername(sendUsername);
//					message1.setRecusername(peoples[i]);
//					message1.setValue("您于<"+meetingTime+">日，有一个会议,请登陆会议管理模块进行查看!");
					//TjsMessage.send(message1);
					
					TjsMessageIm messageIm = new TjsMessageIm();
					messageIm.sendMessage(peoples[i], "您于<"+meetingTime+">日，有一个会议,请登陆会议管理模块进行查看!");
				}
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			jdbcdao.closeStmt();
			Connection con = jdbcdao.getConn();
			try {
				if (!con.isClosed()) {
					jdbcdao.closeConn();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//修改查阅状态
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.meetingroom.bo.IMeetingRoomInceptBo#update(java.lang.String, int)
	 */
	public String update(String employ_id,int userid) {
		Date date=new Date();
		
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String dates = dateFormat.format(date);
	
		String sql = "update t_meetingRoom_incept set datetime=now(),state='1'  where employ_id="+employ_id+" and receiveUser_id='"+userid+"'";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
		return dates;
	}
	//删除发送内容
/* (non-Javadoc)
 * @see com.tjsinfo.tjsoa.meetingroom.bo.IMeetingRoomInceptBo#delete(java.lang.String)
 */
public void delete(String i) {
	String sql="delete t_meetingRoom_incept wbere where id='"+i+"'";
	jdbcdao.executeUpdate(sql);
	jdbcdao.closeStmt();
	jdbcdao.closeConn();
}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.meetingroom.bo.IMeetingRoomInceptBo#getJdbcdao()
	 */
	public JDBCDao getJdbcdao() {
		return jdbcdao;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.meetingroom.bo.IMeetingRoomInceptBo#setJdbcdao(com.tjsinfo.tjsoa.Dao.JDBCDao)
	 */
	public void setJdbcdao(JDBCDao jdbcdao) {
		this.jdbcdao = jdbcdao;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.meetingroom.bo.IMeetingRoomInceptBo#getDai(com.tjsinfo.tjsoa.system.vo.TSystemUser)
	 */
	public List getDai(MeetingRoomEmploy employ) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MeetingIncept.class);
		criteria.add(Restrictions.eq("meetingRoomEmploy", employ));
		criteria.addOrder(Order.asc("state"));
		criteria.addOrder(Order.desc("datetime"));
		return qbcDao.getCriteria(criteria);
	}
	
	public Integer getMainDai(TSystemUser user) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MeetingIncept.class);
		criteria.add(Restrictions.and(Restrictions.eq("receiveUserId", user.getUserId()), Restrictions.eq("state", 0)));
		return qbcDao.getCriteria(criteria).size();
	}
}
